﻿using Furion.CMS.Core;
using Furion.CMS.Core.EventSource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    /// <summary>
    /// 外部事件中心
    /// <para>转发到外部前端应用</para>
    /// </summary>
    public interface IOuterEventHub
    {
        /// <summary>
        /// 获取所有用户注册的外部事件监听
        /// </summary>
        /// <returns></returns>
        List<IOuterEventProvider> GetAll();
        /// <summary>
        /// 获取指定用户的外部事件监听
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        List<IOuterEventProvider> Get(string userId);
        /// <summary>
        /// 注册外部事件
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="channelId">通道id</param>
        /// <returns></returns>
        IOuterEventListener Register(string userId, Guid channelId);
        /// <summary>
        /// 卸载外部事件
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="channelId">通道id</param>
        void UnRegister(string userId, Guid channelId);
    }

    public interface IOuterEventProvider
    {
        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="notify"></param>
        void Publish(OuterEventModel notify);
    }

    /// <summary>
    /// 外部事件监听
    /// </summary>
    public interface IOuterEventListener : IDisposable
    {
        /// <summary>
        /// 等待触发
        /// </summary>
        /// <returns></returns>
        Task<OuterEventModel> WaitTrigger(CancellationToken token = default);
        /// <summary>
        /// 监听状态
        /// </summary>
        bool State { get; }
        /// <summary>
        /// 开始监听
        /// </summary>
        void Start();
        /// <summary>
        /// 结束监听
        /// </summary>
        void Stop();
    }
}
